package top.littlejiang.onlinexam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.config.CronTask;
import top.littlejiang.onlinexam.common.configuration.TaskConfig;
import top.littlejiang.onlinexam.common.handler.OpenExamHandler;
import top.littlejiang.onlinexam.common.handler.base.BaseExamHandler;
import top.littlejiang.onlinexam.common.utils.DateUtils;
import top.littlejiang.onlinexam.common.utils.JSONUtils;
import top.littlejiang.onlinexam.common.utils.UUIDUtil;
import top.littlejiang.onlinexam.entity.OlexamTask;
import top.littlejiang.onlinexam.mapper.OlexamTaskMapper;
import top.littlejiang.onlinexam.service.OlexamTaskService;
import top.littlejiang.onlinexam.common.base.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 定时任务 服务实现类
 * </p>
 *
 * @author chen
 * @since 2021-04-02
 */
@Service
public class OlexamTaskServiceImpl extends BaseServiceImpl<OlexamTaskMapper, OlexamTask> implements OlexamTaskService {

    @Autowired
    TaskConfig taskConfig;
    @Autowired
    OlexamTaskMapper taskMapper;
    @Autowired
    Map<String, BaseExamHandler> handlerMap;

    /**
     * 新增任务
     * 参数需要 fkId corn type
     * @param taskDo
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @Override
    public void addTask(OlexamTask taskDo) throws IllegalAccessException, InstantiationException {
        String taskId = UUIDUtil.getUUID();
        taskDo.setTaskId(taskId)
                .setTaskParams(JSONUtils.beanToJson(taskDo.getParamMap()))
                .setCreateTime(DateUtils.getTime())
                .setDelay(0);
        taskMapper.insert(taskDo);

        this.addOne(taskDo);
    }

    private void addOne(OlexamTask taskDo) throws IllegalAccessException, InstantiationException {
        BaseExamHandler examHandler = handlerMap.get(taskDo.getTaskType());
        BaseExamHandler examHandler1 = examHandler.getClass().newInstance();
        examHandler1.initTask(taskDo);

        CronTask cornAdd = new CronTask(
                examHandler1, taskDo.getTaskCorn());
        taskConfig.add(taskDo, cornAdd);
    }

    /**
     * 更新任务 --> 更新数据库，重新添加任务线程
     * @param task
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    @Override
    public void updateTask(OlexamTask task) throws InstantiationException, IllegalAccessException {
        taskConfig.delete(task.getTaskId());
        taskMapper.updateById(task);
        this.addOne(task);
    }

    /**
     * 删除任务
     *
     * @param taskId
     */
    @Override
    public void deleteTask(String taskId) {
        taskConfig.delete(taskId);
    }

    /**
     * 删除任务
     *
     * @param taskIds
     */
    @Override
    public void deleteTasks(List<Serializable> taskIds) {
        taskIds.stream().forEach(item -> {
            taskConfig.delete((String)item);
        });
    }

    /**
     * 根据fkId 获取id 列表
     *
     * @param fkId
     * @return
     */
    @Override
    public List<Serializable> selectIdByfk(String fkId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("task_id");
        wrapper.eq("fk_id", fkId);
        List<Serializable> list = listObjs(wrapper);
        return list;
    }

    /**
     * 根据fkId 获取详细列表
     *
     * @param fkId
     * @return
     */
    @Override
    public List<OlexamTask> selectByfk(String fkId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("fk_id", fkId);
        List<OlexamTask> list = list(wrapper);
        return list;
    }
}
